﻿using Common.Dtos;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using Sigma.Admin.Client.Extensions;
using Sigma.Admin.Client.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sigma.Admin.Client.ViewModels
{
    public class LoginViewModel : BindableBase, IDialogAware
    {
        public LoginViewModel(ILoginService loginService, IEventAggregator aggregator)
        {
            ExecuteCommand = new DelegateCommand<string>(Execute);
            this.loginService = loginService;
            this.aggregator = aggregator;
        }

        public string Title { get; set; } = "Sigma_Admin";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            LoginOut();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
        }

        private void Execute(string obj)
        {
            switch (obj)
            {
                case "Login": Login(); break;
                case "LoginOut": LoginOut(); break;
                case "Exchange": AccountChange(); break;
                case "Load":OnLoading();break;
                case "UnLoad": UnLoading(); break;

            }
        }

        #region Mock数据切换
        public static int current = 0;
        public static int Current
        {
            get { if(current>3)
                    current=0;
                return current++; }
        }
        public (string account,string psw) NextAccount()
        {
            var cur = Current;
            switch (cur)
            {
                case 0:
                    return ("admin", "admin123");
                case 1:
                    return ("superAdmin", "super123");
                case 2:
                    return ("normalUser", "user123");
                case 3:
                    return ("specialUser", "special123");
            }
            return (null,null); 
        }
        public void AccountChange()
        {
            var user = NextAccount();
            UserName = user.account;
            PassWord = user.psw;
        }
        #endregion

        #region about登录显示
        private bool _isLoading = false;
        public bool IsLoading
        {
            get { return _isLoading; }
            set { _isLoading = value; RaisePropertyChanged(); }
        }
        private string _btnContent = "登录系统";
        public string BtnContent
        {
            get { return _btnContent; }
            set { _btnContent = value; RaisePropertyChanged(); }
        }
        public void OnLoading()
        {
            IsLoading = true;
            BtnContent = "登录中...";
        }
        public void UnLoading()
        {
            IsLoading = false;
            BtnContent = "登录系统";
        }
        #endregion

        #region Login登录逻辑

        private int selectIndex;

        public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }


        public DelegateCommand<string> ExecuteCommand { get; private set; }


        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }

        private string passWord;
        private readonly ILoginService loginService;
        //事件聚合器
        private readonly IEventAggregator aggregator;

        public string PassWord
        {
            get { return passWord; }
            set { passWord = value; RaisePropertyChanged(); }
        }

        



        async void Login()
        {

            if (string.IsNullOrWhiteSpace(UserName) ||
                string.IsNullOrWhiteSpace(PassWord))
            {
                return;
            }
            OnLoading();
            var loginResult = await loginService.Login(new LoginUserDto()
            {
                Account = UserName,
                PassWord = PassWord
            });

            if (loginResult != null && loginResult.Status)
            {
                //结束窗口逻辑，返回成功状态码
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                aggregator.SendMessage($"欢迎回来，{AppSession.Instance.UserName}");

            }
            else
            {
                var mes = loginResult.Message ?? "空信息";
                //登录失败提示...
                aggregator.SendMessage(mes, "Login");
            }

            UnLoading();
            //RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
        }

        //返回cancel状态码
        void LoginOut()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }

        #endregion
    }

    
}
